package com.wu01.demo.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Controller
@Slf4j
public class FileController {

    @PostMapping("/upload")
    public String fileUpload(@RequestParam("email") String email,
                             @RequestParam("username") String name,
                             @RequestPart("header") MultipartFile header,
                             @RequestPart("photos") MultipartFile[] photos)  {
        log.info("email is {}, name is {}, head is {}, photos is {}",email,name,header,photos);
        if(header != null){
            String originalFilename = header.getOriginalFilename();
            try {
                header.transferTo(new File("d:\\data\\cache\\"+originalFilename) );
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(photos.length > 0){
            for (MultipartFile file : photos){
                if(! file.isEmpty()){
                    String originalFilename = file.getOriginalFilename();
                    try {
                        file.transferTo(new File("d:\\data\\cache\\"+originalFilename));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return "badfdfsdfafaaagin1111";
    }

    @GetMapping("/profile")
    public String profile(/*Model model*/){
//        model.addAttribute("settings","#settings");

//        URI uri = request.getURI();
//        System.out.println(uri);
        return "myfile/profile";
    }
//    @GetMapping("/profile#settings")
//    public String profileSetttings(){
//        return "file/profile#settings";
//    }
}
